package com.primary.controller;

import com.primary.common.Result;
import com.primary.controller.request.HistoryRequest;
import com.primary.entity.History;
import com.primary.entity.dto.HistoryAndDonoinfoDto;
import com.primary.entity.dto.HistoryDto;
import com.primary.service.HistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/history")
public class HistoryController {
    @Autowired
    HistoryService historyService;

    @RequestMapping(value = "/page",method = {RequestMethod.GET})
    public Result page(HistoryRequest historyRequest){
        return Result.success(historyService.queryByPage(historyRequest));
    }

    @RequestMapping(value = "/addOne", method = {RequestMethod.POST})
    public Result insertOneUser(@RequestBody HistoryAndDonoinfoDto historyAndDonoinfoDto) {
        if (historyAndDonoinfoDto.getDonors() != null &&
                historyAndDonoinfoDto.getBloodType() !=null) {
            return historyService.insertOne(historyAndDonoinfoDto) > 0 ?
                    Result.success() : Result.error("-114","插入错误");
        } else {
            return Result.error("请输入所有信息");
        }
    }

    @ResponseBody
    @RequestMapping(value = "/updateOne",method = {RequestMethod.PUT})
    public Result updateOne(@RequestBody History history){
        if (null != history.getDonors() &&
                null != history.getBloodType()){
            return historyService.updateOneById(history) > 0 ?
                    Result.success() : Result.error("更新失败!");
        } else {
            return Result.error("信息不能为空！");
        }
    }

    @RequestMapping(value = "/deleteOne/{id}",method = {RequestMethod.DELETE})
    public Result deleteById(@PathVariable Integer id){
        return null == id ? Result.error("ID不能为空") :
                (historyService.deleteById(id) ? Result.success():Result.error("删除失败"));
    }

    @RequestMapping(value = "/export/{actualname}", method = {RequestMethod.GET})
    public Result allHisandinfo(@PathVariable String actualname) {
        List<HistoryDto> list = historyService.exportInfo(actualname);
        return (list == null || list.isEmpty()) ?
                Result.error("-1","未查询到相关信息"):Result.success(list);
    }
}
